
var MapDataMd = require("TiledMapData")
var Loader = require("wy-Loader")
var g_MapAreaLineNode={}
var g_MapAreaRowNode={}
var g_MapDataIndexs=[]
cc.Class({
    extends: cc.Component,
    properties: 
    {
       AreaWidth:
       {
           get()
           {
               if(!this._AreaWidth)this._AreaWidth=1280;
               return this._AreaWidth;
           },
           set(value)
           {
               this._AreaWidth = value;
               this.setContentSize(cc.size(this.AreaWidth,this.AreaHeight));
           }
       },
       AreaHeight:
       {
           get(){
               if(!this._AreaHeight)this._AreaHeight=768;
               return this._AreaHeight;
           },
           set(value)
           {
               this._AreaHeight=value;
               this.setContentSize(cc.size(this.AreaWidth,this.AreaHeight));
           }
       },
       CellWidth:{
           get(){
               if(!this._CellWidth)this._CellWidth=64;
               return this._CellWidth;
           },
           set(value)
           {
               this._CellWidth=value;
           }
       },
       CellHeight:
       {
        get()
        {
            if(!this._CellHeight)this._CellHeight=64;
            return this._CellHeight;
        },
        set(value)
        {
            this._CellHeight=value;
        }
       },
       CurrentRow:
       {
           get()
           {
               if(!this._CurrentRow)this._CurrentRow = 0;
               return this._CurrentRow;
           },
           set(value)
           {
               this._CurrentRow = -1*value;
               this.UpdatePosition();
               MapDataMd.QueryMapData(this.CurrentLine+this.CenterLine,this.CurrentRow+this.CenterRow,this.UpdateMapData.bind(this));
               
           }
       },
       CurrentLine:
       {
           get()
           {
               if(!this._CurrentLine)this._CurrentLine=0;
               return this._CurrentLine;
           },
           set(value)
           {
                this._CurrentLine = value;
                this.UpdatePosition();
                MapDataMd.QueryMapData(this.CurrentLine+this.CenterLine,this.CurrentRow+this.CenterRow,this.UpdateMapData.bind(this));
                
           }
       },
       PlayerRow:
       {
           get()
           {
               if(!this._PlayerRow)this._PlayerRow=0;
               return this._PlayerRow;
           },
           set(value)
           {
               this._PlayerRow=value;
               cc.log(" PlayerLine:",this.PlayerLine," PlayerRow:",this.PlayerRow)
           }
       },
       PlayerLine:
       {
            get()
            {
                if(!this._PlayerLine)this._PlayerLine=0;
                return this._PlayerLine;
            },
            set(value)
            {
                this._PlayerLine=value;
                cc.log(" PlayerLine:",this.PlayerLine," PlayerRow:",this.PlayerRow)
            }
        },
       CenterRow:
       {
           get()
           {
               if(!this._CenterRow)this._CenterRow=0;
               return this._CenterRow;
           },
           set(value)
           {
               this._CenterRow=value;
           }
       },
       CenterLine:
       {
           get()
           {
               if(!this._CenterLine)this._CenterLine=0;
               return this._CenterLine;
           },
           set(value)
           {
               this._CenterLine=value;
           }
       },
       TiledMapSize:
       {
           get()
           {
               if(!this._TiledMapSize)this._TiledMapSize=3;
               return this._TiledMapSize;
           },
           set(value)
           {
               this._TiledMapSize=value;
           }
       },
       MoveStep:
       {
           get()
           {
               if(!this._MoveStep)this.MoveStep=50;
               return this._MoveStep;
           },
           set(value)
           {
               this._MoveStep=value;
           }
       }
    },
    onLoad () 
    {
        g_MapDataIndexs=new Array();
        for(var index=0;index<this.TiledMapSize;index++)g_MapDataIndexs.push(index);
        Loader.LoadImgResources(MapDataMd.getIconUrls(),this.LoadTiledArea.bind(this),()=>{});
    },
    LoadTiledArea()
    {
        var loadFinish = function(error,resource)
        {
            if(!resource) 
            {
                cc.error("Load LoadPrefab Error:"+error);
                return ;
            }
            var tdRoot = this.node.getChildByName("UI-TiledMapRoot");
            for(var line=0;line<this.TiledMapSize;line++)
            {
                if(!g_MapAreaLineNode[line])g_MapAreaLineNode[line]={};
                for(var row=0;row<this.TiledMapSize;row++)
                {
                    if(!g_MapAreaRowNode[row])g_MapAreaRowNode[row]={};
                    var ndName = ""+(line*this.TiledMapSize+row);
                    var tdNd = tdRoot.getChildByName(ndName);
                    if(!tdNd)
                    {
                        tdNd = cc.instantiate(resource);
                        tdNd.name=ndName;
                        if(!tdNd.getComponent("UI-TiledArea"))tdNd.addCompnent("UI-TiledArea");
                        tdNd.getComponent("UI-TiledArea").CellWidth=this.CellWidth;
                        tdNd.getComponent("UI-TiledArea").CellHeight=this.CellHeight;
                        tdNd.getComponent("UI-TiledArea").CurrentMapID=ndName;
                        tdNd.getComponent("UI-TiledArea").OnStateChange=this.OnStateChange;
                        tdRoot.addChild(tdNd);
                    }
                    g_MapAreaLineNode[line][row]=tdNd;
                    g_MapAreaRowNode[row][line]=tdNd;
                }
            }
            this.UpdatePosition();
            setTimeout(()=>
            {
                MapDataMd.QueryMapInitData(this.UpdateMapData.bind(this));
            },100);
        }
        cc.loader.loadRes("./prefab/TiledMap/UI-TiledArea",(completedCount,totalCount,item)=>{},loadFinish.bind(this));
    },
    UpdatePosition()
    {
        
        var xPos={};
        for(var row=0;row<this.TiledMapSize;row++)
        {
            if(this.CurrentRow>0)
            {
                xPos[row]=-1*(-1*this.CurrentRow+(Math.floor(0.5*this.TiledMapSize)-row))*this.AreaWidth;
            }else
            {
                xPos[row]=(this.CurrentRow+(row-(Math.floor(0.5*this.TiledMapSize))))*this.AreaWidth;
            }
        }
        var yPos={};
        for(var line = 0;line < this.TiledMapSize;line++)
        {
            if(this.CurrentLine>0)
            {
                yPos[line] =-1*(this.CurrentLine+(line-(Math.floor(0.5*this.TiledMapSize))))*this.AreaHeight;
            }else
            {
                yPos[line] = (-1*this.CurrentLine+(Math.floor(0.5*this.TiledMapSize)-line))*this.AreaHeight;
            }
        }
        var lineInfo =this.GetIndexInfo(this.CurrentLine);
        var rowInfo = this.GetIndexInfo(this.CurrentRow);

        for(var line=0;line<this.TiledMapSize;line++)
        {
            var lineIdx=lineInfo[line];
            for(var row =0; row < this.TiledMapSize;row++)
            {
                var rowIdx = rowInfo[row];
                var rowNd = g_MapAreaLineNode[lineIdx][rowIdx];
                rowNd.setPosition(cc.v2(xPos[row],yPos[line]));
            }
        }        
    },
    GetIndexInfo(lineOrRow)
    {
        var Data=[];
        var StartIDX=lineOrRow >= 0 ? lineOrRow % this.TiledMapSize : this.TiledMapSize - ((lineOrRow*-1)%this.TiledMapSize);
        StartIDX=StartIDX >= this.TiledMapSize ? 0 : StartIDX;
        for(var index =0;index<this.TiledMapSize;index++)
        {
            Data.push(g_MapDataIndexs[StartIDX]);
            StartIDX=((StartIDX+1)>=g_MapDataIndexs.length) ? 0 : StartIDX+1;
        }
        return Data
    },
    SelectTiledItem(xPos,yPos)
    {

    },
    OnStateChange(data)
    {
        MapDataMd.SetSelectState(data);
    },
    UpdateTiledMapPosition(xPos,yPos)
    {
        this.CurrentRow = Math.trunc(xPos/(this.AreaWidth));
        this.CurrentLine =  Math.trunc(yPos/(this.AreaHeight));
        if(MapDataMd.CheckCanMoveData(this.CurrentLine+this.CenterLine,this.CurrentRow+this.CenterRow) == false ) return ;
        this.node.getChildByName("UI-TiledMapRoot").setPosition(cc.v2(xPos,yPos));
        xPos=xPos-0.5*this.AreaWidth;
        yPos=yPos+0.5*this.AreaHeight;
        xPos=xPos%this.AreaWidth;
        yPos=yPos%this.AreaHeight;
        var row = Math.abs(Math.trunc(xPos/this.CellWidth));
        this.PlayerRow = xPos < 0 ? row : (this.AreaWidth/this.CellWidth)-row;
        var line = Math.abs(Math.trunc(yPos/this.CellHeight));
        this.PlayerLine = yPos < 0 ? (this.AreaHeight/this.CellHeight)-line:line;
    },
    MoveTiledMap(xDir,yDir)
    {
        var pos = this.node.getChildByName("UI-TiledMapRoot").getPosition();
        pos.x = pos.x + xDir*this.MoveStep;
        pos.y = pos.y + yDir*this.MoveStep;
        this.UpdateTiledMapPosition(pos.x,pos.y);
    },
    UpdateMapData(data)
    {
        if(!this.CenterLine)this.CenterLine = data.CenterLine;
        if(!this.CenterRow)this.CenterRow = data.CenterRow;
        var lineInfo =this.GetIndexInfo(this.CurrentLine);
        var rowInfo = this.GetIndexInfo(this.CurrentRow);
        for(var line =0; line < this.TiledMapSize;line++)
        {
            var lineIdx=lineInfo[line];
            for(var row =0; row < this.TiledMapSize; row++)
            {
                var rowIdx=rowInfo[row];
                var tnd = g_MapAreaLineNode[lineIdx][rowIdx];
                tnd.getComponent("UI-TiledArea").UpdateMapAreaData(data[line][row]);
            }
        }
    },
});
